package com.situ.cart.api.service.Impl;

import com.mall.common.util.JsonResult;
import com.mall.common.util.XinUtils;
import com.mall.model.Product;
import com.situ.cart.api.mapper.CartItemMapper;
import com.situ.cart.api.service.ProductFeignService;
import com.situ.cart.spi.model.CartItem;
import com.situ.cart.spi.service.CartItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CartItemServiceImpl implements CartItemService {
    private CartItemMapper cartItemMapper;
    private ProductFeignService productFeignService;

    @Autowired
    public void setProductFeignService(ProductFeignService productFeignService){
        this.productFeignService = productFeignService;
    }

    @Autowired
    public void setCartItemMapper(CartItemMapper cartItemMapper) {
        this.cartItemMapper = cartItemMapper;
    }

    @Override
    public List<CartItem> findByUserId(Integer userId) {
        CartItem ci = new CartItem();
        ci.setUserId(userId);

        List<CartItem> items = cartItemMapper.findBy(ci);
        items.forEach(this::findProduct);
        return items;
    }

    

    @Override
    public CartItem findById(Integer id) {
        CartItem ci = cartItemMapper.selectById(id);
        findProduct(ci);//记得修改回来
        return ci;
    }

    private void findProduct(CartItem ci) {
        ResponseEntity<JsonResult<Product>> resp = productFeignService.findById(ci.getProductId());
        JsonResult<Product> jr = resp.getBody();
        if (jr != null && jr.isSuccess()) {
            ci.setProduct(jr.getData());//设置商品
        }
    }

    @Override
    public boolean save(CartItem cartItem) {
        List<CartItem> items = cartItemMapper.findBy(cartItem);
        if (XinUtils.isNotEmpty(items)) {
            CartItem ci = items.getFirst();
            ci.setQuantity(ci.getQuantity() + cartItem.getQuantity());//增加数量
            return cartItemMapper.updateById(ci) > 0;
        } else {
            return cartItemMapper.insert(cartItem) > 0;
        }
    }

    @Override
    public boolean update(CartItem cartItem) {
        return cartItemMapper.updateById(cartItem) > 0;
    }

    @Transactional(rollbackFor = Exception.class,
            isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRES_NEW)
    @Override
    public int deleteByIds(List<Integer> ids) {
        int count = 0;
        for (Integer id : ids) {
            count += cartItemMapper.deleteById(id);
        }
        return count;
    }

    @Override
    public int[] findByProductId(Integer id) {
        return cartItemMapper.findByProductId(id);
    }

    @Override
    public int clearAllByUserId(Integer userId) {
        return cartItemMapper.clearAllByUserId(userId);
    }
}
